package com.company;

import com.utils.*;

import java.io.IOException;
import java.util.Scanner;

public class TreeCRUD {

    public void treeCRUD(){
        System.out.println("平衡二叉树的增删改查！！！");
    }

    /**
     * 平衡二叉树文件读取
     * @return
     * @throws IOException
     */
    public MyALVTree treeReadFile() throws IOException {

        FileUtilsForTree fileUtilsForTree = new FileUtilsForTree();
        MyALVTree tree = fileUtilsForTree.readFile("src\\com\\data\\Test_ORDER.txt");

        return tree;
    }

    /**
     * 打印出平衡二叉树读取的文件
     * @throws IOException
     */
    public void treeReadFilePrint() throws IOException {
        MapData.MyTree = treeReadFile();
        System.out.println("文件读取完毕！！！");
        MapData.MyTree.innerTraverse(MapData.MyTree.root);
        System.out.println("平衡二叉树内数据打印完毕！！");
    }


    /**
     * 平衡二叉树插入道路信息！！
     * @throws IOException
     */
    public void treeInsert() throws IOException{

        System.out.println("平衡二叉树插入道路信息！！！");
        System.out.println("请分别输入道路信息中的：linkID、roadnameflag、branch、dispclass、roadname");
        //    #linkID=55410;roadnameflag=0;branch=0;dispclass=13;roadname=
        Map insertMap = new Map();
        System.out.print("请输入LinkId:");
        Scanner sc = new Scanner(System.in);
        int linkID = sc.nextInt();
        insertMap.setLinkID(linkID);
        System.out.print("请输入roadnameflag：");
        int roadnameflag = sc.nextInt();
        insertMap.setRoadnameflag(roadnameflag);
        System.out.print("请输入branch：");
        int branch = sc.nextInt();
        insertMap.setBranch(branch);
        System.out.print("请输入dispclass：");
        int dispclass = sc.nextInt();
        insertMap.setDispclass(dispclass);
        System.out.print("请输入roadname：");
        String roadname = sc.next();
        insertMap.setRoadname(roadname);

        MapData.MyTree.insert(MapData.MyTree.root,insertMap);

        MapData.MyTree.innerTraverse(MapData.MyTree.root);

        System.out.println("插入完毕！！！");
    }


    public void treeSerch() throws IOException{

        int linkID = -1;
        System.out.println("平衡二叉树查询");
        System.out.print("请输入需要查询的LinkID:");
        Scanner sc = new Scanner(System.in);
        linkID = sc.nextInt();

        //查询语句
        MapData.MyTree.findMapByLinkID(MapData.MyTree.root,linkID);

        Map map = MapData.MyTree.findMapByLinkID(MapData.MyTree.root,linkID);
        System.out.println(map.toString());

    }

    public void treeUpdate(){

        System.out.println("平衡二叉树更改！");

        //    #linkID=55410;roadnameflag=0;branch=0;dispclass=13;roadname=
        Map updateMap = new Map();

        Scanner sc = new Scanner(System.in);
        System.out.print("请输入LinkId:");
        int linkID = sc.nextInt();
        updateMap.setLinkID(linkID);

        System.out.println("查询到该linkID对应的数据:" + MapData.MyTree.findMapByLinkID(MapData.MyTree.root,linkID).toString());

        System.out.println("请分别输入道路信息中的：roadnameflag、branch、dispclass、roadname");
        System.out.print("请输入roadnameflag：");
        int roadnameflag = sc.nextInt();
        updateMap.setRoadnameflag(roadnameflag);
        System.out.print("请输入branch：");
        int branch = sc.nextInt();
        updateMap.setBranch(branch);
        System.out.print("请输入dispclass：");
        int dispclass = sc.nextInt();
        updateMap.setDispclass(dispclass);
        System.out.print("请输入roadname：");
        String roadname = sc.next();
        updateMap.setRoadname(roadname);

        MapData.MyTree.updateTreeByLinkID(MapData.MyTree.root,linkID,updateMap);

        MapData.MyTree.innerTraverse(MapData.MyTree.root);

        System.out.println("平衡二叉树内数据更改完毕！！！");
    }



    public void treedDelete(){

        System.out.println("平衡二叉树删除！");

        Scanner sc = new Scanner(System.in);
        System.out.print("请输入LinkId:");
        int linkID = sc.nextInt();

        MapData.MyTree.deleteAVL(linkID);

        MapData.MyTree.innerTraverse(MapData.MyTree.root);

        System.out.println("删除完毕！！！");
    }



}
